using System;
using System.Web;
using System.Web.SessionState;
using TheBlog.NhibernateModel.Entities;
using NHibernate.Envers;

namespace TheBlog.NhibernateModel
{
    public class RevisionListener : IRevisionListener
    {
        public void NewRevision(object revisionEntity)
        {
            var casted = revisionEntity as ArgeLogRevInfo;

            if (casted != null)
            {
                casted.UserId = GetLoginnedUserId();
                casted.RawUrl = GetRawUrl();
                casted.Controller = GetController();
                casted.Action = GetAction();
                casted.BrowserInfo = GetBrowserInfo();
                casted.Ip = GetIp();
                casted.SessionId = GetSessionId();
            }
        }

        private string GetSessionId()
        {
            if (HttpContext.Current == null)
                return null;
            return HttpContext.Current.Session.SessionID;
        }
        private string GetIp()
        {
            if (HttpContext.Current == null)
                return null;
            return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        private Guid? GetLoginnedUserId()
        {
            HttpSessionState session;
            if (HasSession(out session))
                return null;
            var userSessionValue = session["UserId"];
            if (userSessionValue == null)
                return null;
            return (Guid?)userSessionValue;
        }
        private static bool HasSession(out HttpSessionState session)
        {
            if (HttpContext.Current == null)
            {
                session = null;
                return true;
            }
            session = HttpContext.Current.Session;
            if (session == null)
                return true;
            return false;
        }
        private string GetRawUrl()
        {
            if (HttpContext.Current == null)
                return null;
            return HttpContext.Current.Request.Url.OriginalString;
        }
        private string GetController()
        {
            if (HttpContext.Current == null)
                return null;
            return HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("controller");
        }
        private string GetAction()
        {
            if (HttpContext.Current == null)
                return null;
            return HttpContext.Current.Request.RequestContext.RouteData.GetRequiredString("action");
        }
        private string GetBrowserInfo()
        {
            if (HttpContext.Current == null)
                return null;
            var context = HttpContext.Current;
            return
                "RemoteUser=" + context.Request.ServerVariables["REMOTE_USER"] + ";\n"
                + "RemoteHost=" + context.Request.ServerVariables["REMOTE_HOST"] + ";\n"
                + "Type=" + context.Request.Browser.Type + ";\n"
                + "Name=" + context.Request.Browser.Browser + ";\n"
                + "Version=" + context.Request.Browser.Version + ";\n"
                + "MajorVersion=" + context.Request.Browser.MajorVersion + ";\n"
                + "MinorVersion=" + context.Request.Browser.MinorVersion + ";\n"
                + "Platform=" + context.Request.Browser.Platform + ";\n"
                + "SupportsCookies=" + context.Request.Browser.Cookies + ";\n"
                + "SupportsJavaScript=" + context.Request.Browser.EcmaScriptVersion.ToString() + ";\n"
                + "SupportsActiveXControls=" + context.Request.Browser.ActiveXControls + ";\n"
                + "SupportsJavaScriptVersion=" + context.Request.Browser["JavaScriptVersion"] + "\n";
        }
    }
}